package com.syqc.custom

import android.content.Context
import android.os.Build
import android.text.Html
import android.text.InputFilter
import android.text.InputFilter.LengthFilter
import android.text.method.DigitsKeyListener
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.EditText
import android.widget.FrameLayout
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import androidx.core.view.isGone
import com.syqc.comlib.rx.doAfterChanged
import com.syqc.monitor.R
import com.syqc.monitor.databinding.LayoutLrEditBinding


class UnitEditText : FrameLayout {
    private lateinit var binding: LayoutLrEditBinding

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initView(context, attrs)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(
        context, attrs, defStyle
    ) {
        initView(context, attrs)
    }

    private fun initView(context: Context, attrs: AttributeSet?) {
        binding = LayoutLrEditBinding.inflate(LayoutInflater.from(context), this, true)
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.unit_edit)

        val bottomGone = typedArray.getBoolean(R.styleable.unit_edit_bottomGone, false)
        val unitGone = typedArray.getBoolean(R.styleable.unit_edit_unitGone, false)
        val editWeight = typedArray.getFloat(R.styleable.unit_edit_editWeight, 2.5f)
        val descText = typedArray.getString(R.styleable.unit_edit_descText)
        val editText = typedArray.getString(R.styleable.unit_edit_editText)
        val editHint = typedArray.getString(R.styleable.unit_edit_editHint)
        val unitText = typedArray.getString(R.styleable.unit_edit_unitText)
        val edigits = typedArray.getString(R.styleable.unit_edit_edigits)
        val elength = typedArray.getInt(R.styleable.unit_edit_elength, 8)
        val textColor = typedArray.getColor(
            R.styleable.unit_edit_textColor, ContextCompat.getColor(context, R.color.color_333)
        )

        binding.tvDesc.text = descText
        binding.editInput.setText(editText)
        binding.editInput.hint = editHint
        binding.lineBottom.isGone = bottomGone
        binding.tvUnit.isGone = unitGone
        binding.tvUnit.text = unitText

        binding.tvDesc.setTextColor(textColor)
        binding.editInput.setTextColor(textColor)
        if (edigits != null)
            binding.editInput.keyListener = DigitsKeyListener.getInstance(edigits)

        val params = LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT)
        if (unitGone) {
            params.weight = editWeight + 0.5f
        } else {
            params.weight = editWeight
        }
        binding.editInput.layoutParams = params

        binding.editInput.filters = arrayOf(LengthFilter(elength))
        typedArray.recycle()
    }

    fun setDesc(desc: String) {
        binding.tvDesc.text = desc
    }

    fun setDescStar(desc: String) {
        val htmlStr = "<font color='#111111'>$desc</font><font color='#FF0000'>*</font>"
        binding.tvDesc.text = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Html.fromHtml(htmlStr, Html.FROM_HTML_MODE_LEGACY)
        } else {
            Html.fromHtml(htmlStr)
        }
    }

    fun setDesc(descId: Int) {
        binding.tvDesc.text = context.getString(descId)
    }

    fun unitGone(isGone: Boolean) {
        binding.lineBottom.isGone = isGone
    }

    fun setHint(hint: String) {
        binding.editInput.hint = hint
    }

    fun setUnit(unitText: String) {
        binding.tvUnit.text = unitText
    }

    fun setKeyListener(keys: String) {
        binding.editInput.keyListener = DigitsKeyListener.getInstance(keys)
    }

    fun getEditText(): EditText {
        return binding.editInput
    }

    fun setText(text: String) {
        binding.editInput.setText(text)
        binding.editInput.setSelection(binding.editInput.text.length)
    }

    fun setOnTextChange(change: (String) -> Unit) {
        binding.editInput.doAfterChanged {
            change.invoke(it)
        }

    }

    fun setFilter(length: Int) {
        binding.editInput.filters = arrayOf(LengthFilter(length), filter)
    }

    private val filter = InputFilter { source, start, end, dest, dstart, dend ->
        for (i in start until end) {
            if (!Character.isLetterOrDigit(source[i])
                && source[i].toString() != "_"
                && source[i].toString() != "-"
            ) {
                return@InputFilter ""
            }
        }
        null
    }
}

